package com.tenbent.product.base.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * 
 * 
 * @author Randy Yan
 *
 * @CreateDate 2009 2009-8-29 上午12:19:48
 *
 */
public class DateUtil {

	public static final String PATTERN = "yyyy-MM-dd";
	public static final String PATTERN_LONG = "yyyy-MM-dd HH:mm:ss";
	public static String PATTERN_SHOT = "yyyy/MM/dd";
	public static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

	public static Calendar getCanledar() {
		Calendar c = Calendar.getInstance();
		return c;
	}

	public static int getCurrentYrLong() {
		Calendar c = getCanledar();
		return c.get(Calendar.YEAR);
	}

	public static String getCurrentYrShrt() {
		String yrl = "" + getCurrentYrLong();
		// int len=3;
		String rs = "";
		if (yrl.length() > 1) {
			rs = yrl.substring(yrl.length() - 2, yrl.length());
		}
		// if()
		// String yrs=yrl.substring()
		// int yr=c.get(Calendar.YEAR)-1900;

		return rs;
	}

	public static int getCurrentMth() {
		Calendar c = getCanledar();
		return c.get(Calendar.MONTH) + 1;
	}

	public static int getCurrentDate() {
		Calendar c = getCanledar();
		return c.get(Calendar.DATE);
	}

	/**
	 * 格式化日期，
	 * 
	 * @param date
	 * @return
	 */
	public static String dateFormat(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sf = (SimpleDateFormat) SimpleDateFormat.getDateInstance();

		sf.applyPattern(PATTERN);

		return sf.format(date);

	}

	public static String dateFormat(Date date, String pattern) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sf = (SimpleDateFormat) SimpleDateFormat.getDateInstance();

		sf.applyPattern(pattern);

		return sf.format(date);

	}

	/**
	 * 格式化日期，
	 * 
	 * @param
	 * @return
	 */
	public static Date toDate(String datestr) {
		if (datestr == null || "".equals(datestr.trim())) {
			return null;
		}
		SimpleDateFormat sf = (SimpleDateFormat) SimpleDateFormat.getDateInstance();

		sf.applyPattern(PATTERN);
		Date d;
		try {
			d = sf.parse(datestr);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return d;

	}

	/**
	 * 格式化日期，
	 * 
	 * @param
	 * @param pattern
	 * @return
	 */
	public static Date toDate(String datestr, String pattern) {
		if (datestr == null) {
			return null;
		}
		SimpleDateFormat sf = (SimpleDateFormat) SimpleDateFormat.getDateInstance();

		sf.applyPattern(pattern);
		Date d;
		try {
			d = sf.parse(datestr);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return d;

	}

	/***************************************************************************
	 * 对日期字符串去"-"
	 * 
	 * @param date
	 * @return
	 */
	public static String dateFormat(String date) {
		String[] tmp = date.split("\\-");
		StringBuffer re = new StringBuffer();
		for (int i = 0; i < tmp.length; i++) {
			re.append(tmp[i]);
		}
		return re.toString();
	}

	/**
	 * 根据生日求年龄
	 * 
	 * @param birthday
	 * @return
	 */
	public static int birthdayToAge(Date birthday) {
		if (birthday == null) {
			return 0;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(birthday);
		Calendar now = Calendar.getInstance();

		int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
		if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH)) || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
				&& cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) {
			res--;
		}
		return res;
	}

	/*********
	 * 日期形式的转换，由YYYYMMDD 变成YYYY-MM-DD
	 * 
	 * @param date
	 * @return
	 */
	public static String formatString(String date) {
		if ("".equals(date)) {
			return "";
		} else {
			StringBuffer buffer = new StringBuffer();
			buffer.append(date.substring(0, 4)).append("-").append(date.substring(4, 6)).append("-")
					.append(date.substring(6, 8));
			return buffer.toString();
		}
	}

	/***********
	 * 得到系统的当前日期
	 * 
	 * @return
	 */
	public static String getSystemDate() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-mm-dd");
		return f.format(c.getTime());
	}

	/**
	 * 得到一个日期在增加或减少n天后的日期
	 * 
	 * @param date
	 * @param n
	 *            正数为增加 负数为减少
	 * @return
	 */
	public static Date getNewDate(Date date, int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, n);
		return c.getTime();
	}

	public static int getCountBetweenDate(Date from, Date to) {
		int re = 0;
		re = (int) ((from.getTime() - to.getTime()) / (24 * 60 * 60 * 1000));
		return re;
	}

	public static boolean isDateExpired(Date end) {
		if (end == null)
			return false;
		if (new Date().after(new Date(end.getTime() + 24 * 60 * 60 * 1000))) {
			return true;
		}
		return false;

	}

	public static boolean isDateExpired(Date end, int delta) {
		if (end == null)
			return false;
		if (new Date().after(new Date(end.getTime() + delta * 60 * 1000))) {
			return true;
		}
		return false;

	}

	public static void main(String[] args) {
		// DateUtil u = new DateUtil();
		// System.out.print(DateUtil.getCurrentYrShrt());
		Date d = DateUtil.toDate("1908-10-40");
		System.out.println(DateUtil.birthdayToAge(d));
	}
}
